/**
 * Created by liubin on 2017/2/23.
 */

var WhmjPlayerMyModel = function () {
    var that = new WhmjPlayerModel();

    //选中牌的index
    that.selectInhandCardIndex = null;

    //滑动出牌的牌
    that.selectMoveCard = null;

    //滑动出牌的牌是否显示了
    that.isSelectMoveCardShowOnce = null;

    //自己不能出的牌 (别人听的牌)
    that.selfNotPlayCardNums = [];

    //自己听的牌
    that.selfTingCardNums = [];

    //自己亮听的时候黑掉的牌
    that.selfTingOperatingBlackNums = [];

    //flag
    that.mFlagWeixianNums = [];
    that.mFlagLiangNums = [];
    that.mFlagTingNums = [];

    //染色
    that.cardColorOtherTing = cc.color(160, 160, 160);
    that.cardColorLiang = cc.color(160, 160, 160);
    that.cardColorSelectMove = cc.color(230, 230, 230);
    that.cardColorNotPlaySeat = cc.color(210, 210, 210);


    //监听手牌触摸
    that.loadTouchListener = function(node){
        cc.eventManager.addListener({
            event : cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches : true,
            onTouchBegan:function (touch, event) {
                //抬起或者打出手牌
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);

                //设置手牌位置
                that.setInahndCardPosition();

                //手牌大小

                var cardSize = cc.size(mo.whmjCardHelper.inhandCardSize.width * mo.whmjCardHelper.inhandCardScale, mo.whmjCardHelper.inhandCardSize.height * mo.whmjCardHelper.inhandCardScale);

                for (var i = 0; i < that.inhandCards.length; i++){
                    //手牌位置
                    var card = that.inhandCards[i];
                    var cardPos = mo.whmjCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
                    var rect = cc.rect(cardPos.x, cardPos.y, cardSize.width, cardSize.height);

                    //是否点中手牌
                    if (cc.rectContainsPoint(rect, point)){
                        if (card.mIsColor){
                            //染色了就不响应触摸
                            break;
                        }

                        //如果再次点击已经选中的牌，就打出此牌
                        if (that.setSelectInhandCard(i)){
                            //todo
                            mo.whmjTableStatus.sendPlayCard(card.mCardNum);
                            return false;
                        }else {
                            that.setSelectMoveCard(true, card);
                            return true;
                        }
                    }
                }

                that.setSelectInhandCard(null);

                return false;
            },
            onTouchMoved: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                that.moveSelectMoveCard(point);
            },
            onTouchEnded: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                that.endSelectMoveCard(point);
            },
            onTouchCancelled : function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                that.setSelectMoveCard(false);
            }

        }, node);
    };

    //设置选中牌 返回true 需要打出牌 返回false启动滑动出牌
    that.setSelectInhandCard = function(index){
        if (index != null){
            //选中了牌
            if (index == that.selectInhandCardIndex){
                return true;
            }else{
                that.selectInhandCardIndex = index;
                mo.soundHelper.playMjSelect();
            }
        }else{
            //没有选 取消所有牌的选择
            that.selectInhandCardIndex = null;
        }

        //亮听的时候通知ui显示可以胡哪些牌
        //todo
        // if (mo.kwxTableStatus.useTing){
        //     var cardNum = null;
        //     if (that.selectInhandCardIndex != null){
        //         cardNum = that.inhandCards[that.selectInhandCardIndex].mCardNum;
        //     }
        //     mo.kwxTable.mKwxTableUILayer.mKwxTableOperatingLayer.getTingTimeHuCardNum(cardNum);
        // }

        //牌位置
        that.setInahndCardPosition();

        return false;
    };

    that.setSelectMoveCard = function(isSelect, card){
        if (isSelect){
            that.selectMoveCard = mo.whmjTabel.mWhmjCardLayer.getSelectMoveSprite();
            that.selectMoveCard.setCardNum(card.mCardNum);
            that.selectMoveCard.setCardKind(0, 1);
            that.selectMoveCard.setVisible(false);
            card.setCardColor(that.cardColorSelectMove);
            that.isSelectMoveCardShowOnce = false;
        }else{
            if (that.selectMoveCard){
                that.selectMoveCard.setVisible(false);
            }
            that.startSetInhandCardColor();
            that.selectMoveCard = null;

            if (that.isSelectMoveCardShowOnce){
                that.setSelectInhandCard(null);
            }
        }
    };


    that.moveSelectMoveCard = function(point){
        var selectCardSize = mo.whmjCardHelper.getInhnadCardSize();
        var showPoint = cc.p(point.x - selectCardSize.width / 2, point.y - selectCardSize.height / 2);
        that.selectMoveCard.setPosition(showPoint);
        if (point.y > mo.whmjCardHelper.getInhnadCardSize().height + 10){
            that.selectMoveCard.setVisible(true);
            that.isSelectMoveCardShowOnce = true;
        }else{
            that.selectMoveCard.setVisible(false);
        }
    };


    that.endSelectMoveCard = function(point){
        if (point.y > mo.whmjCardHelper.getInhnadCardSize().height + 10){
            mo.whmjTableStatus.sendPlayCard(that.selectMoveCard.mCardNum);
        }
        that.setSelectMoveCard(false);
    };


    //设置手牌位置 不排序
    that.setInahndCardPosition = function(){
        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            card.stopAllActions();
            card.setOpacity(255);
            var p = mo.whmjCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            if (i == that.selectInhandCardIndex){
                p.y += 30;
            }

            card.setRotation(0);
            card.setPosition(p);
        }
    };


    //手牌染色 唯一染色 有多少个染多少个 不染其他num一样的牌
    //that.setInhandCardColor = function(grayCardNums, yellowCardNums){
        // var useIndexs = [];
        // for (var i = 0; i < that.inhandCards.length; i++){
        //     that.inhandCards[i].resetCardColor();
        // }
        //
        // for (var i = 0; i < grayCardNums.length; i++){
        //     var index = mo.kwxCardHelper.findCardForNumApartIndexs(that.inhandCards, grayCardNums[i], useIndexs);
        //     if (index != null){
        //         that.inhandCards[index].setCardColor(that.cardColorOtherTing);
        //         useIndexs.push(index);
        //     }
        // }
        //
        // useIndexs = [];
        // for (var i = 0; i < yellowCardNums.length; i++){
        //     var index = mo.kwxCardHelper.findCardForNumApartIndexs(that.inhandCards, yellowCardNums[i], useIndexs);
        //     if (index != null){
        //         that.inhandCards[index].setCardColor(that.cardColorLiang);
        //         useIndexs.push(index);
        //     }
        // }
        //
        // //如果自己没有听 而且所有手牌都染色了 就取消所有手牌的染色
        // var selfSeat = mo.kwxTableStatus.mTableStatus["SeatUser" + mo.kwxTableStatus.mTableStatus.MySeat];
        // if (selfSeat){
        //     if (!selfSeat.OpenTing){
        //         var isAllBlack = true;
        //         for (var i = 0; i < that.inhandCards.length; i++){
        //             if (!that.inhandCards.mIsColor){
        //                 isAllBlack = false;
        //                 break;
        //             }
        //         }
        //
        //         if (isAllBlack){
        //             that.setInhandCardColor([], []);
        //         }
        //     }
        // }
        //
        // //如果不该自己出牌 就把所有没有染色的手牌染色
        // if (!mo.kwxTableStatus.getMyIsPlayCard()){
        //     for (var i = 0; i < that.inhandCards.length; i++){
        //         if (!that.inhandCards[i].mIsColor) {
        //             that.inhandCards[i].setCardColor(that.cardColorNotPlaySeat);
        //         }
        //     }
        // }
   // };

    //开始手牌染色
    that.startSetInhandCardColor = function(){
        // //找到手牌中所有的
        // var grayCardNums = [];
        // var findGrayNums = [];
        // if (mo.kwxTableStatus.useTing){
        //     findGrayNums = that.selfTingOperatingBlackNums.concat(that.selfNotPlayCardNums);
        // }else{
        //     findGrayNums = that.selfNotPlayCardNums;
        // }
        // for (var i = 0; i < that.inhandCards.length; i++){
        //     var num = that.inhandCards[i].mCardNum;
        //     var find = mo.gameHelper.findValueForArray(findGrayNums, num);
        //     if (find != null){
        //         grayCardNums.push(num);
        //     }
        // }
        //
        // //有都少个找多少个
        // var yellowCardNums = that.selfTingCardNums;
        //
        // that.setInhandCardColor(grayCardNums, yellowCardNums);
        //
        // return grayCardNums;
   };

    //黑掉指定的牌 听牌操作时候专用
   // that.setTingOperatingBlack = function(blackNums){
        // that.selfTingOperatingBlackNums = blackNums;
        //
        // return that.startSetInhandCardColor();
   // };

    //设置手牌的flag weixianNums全展示 liangNums展示固定数量
   // that.setInhandCardFlag = function(){
        // var weixianNums = that.mFlagWeixianNums;
        // var liangNums = that.mFlagLiangNums;
        // var tingNums = that.mFlagTingNums;
        //
        // //重置一遍
        // for (var i = 0; i < that.inhandCards.length; i++){
        //     that.inhandCards[i].setFlag(-1);
        // }
        //
        // //危险牌
        // for (var i = 0; i < that.inhandCards.length; i++){
        //     if (mo.gameHelper.findValueForArray(weixianNums, that.inhandCards[i].mCardNum) != null){
        //         that.inhandCards[i].setFlag(0);
        //     }
        // }
        //
        // //听的牌
        // for (var i = 0; i < that.inhandCards.length; i++){
        //     if (mo.gameHelper.findValueForArray(tingNums, that.inhandCards[i].mCardNum) != null){
        //         that.inhandCards[i].setFlag(2);
        //     }
        // }
        //
        // //亮的牌
        // var useLiangIndexs = [];
        // for (var i = 0; i < liangNums.length; i++){
        //     var index = mo.kwxCardHelper.findCardForNumApartIndexs(that.inhandCards, liangNums[i], useLiangIndexs);
        //     if (index != null){
        //         that.inhandCards[index].setFlag(1);
        //         useLiangIndexs.push(index);
        //     }
        // }
    //};


    return that;
};